#!/usr/bin/node
// 获取图片的data uri
const fs = require("fs");
const path = require("path");

const argv = process.argv;

const imgPath = argv[2];
if (!imgPath) {
	console.log(`Usage: node ${argv[1]} imagePath`);
}
if (!fs.existsSync(imgPath)) {
	console.error(`image is not exist: ${imgPath}`);
}

const filePath = argv[3];
if (!fs.existsSync(filePath)) {
	console.error(`file is not exist: ${filePath}`);
}

const extname = path.extname(imgPath).substr(1);
if (!["jpg", "jpeg", "png", "gif"].some((e) => e === extname)) {
	console.error(`"${extname}" file is not a valid picture file`);
	return;
}

let bitmap = fs.readFileSync(imgPath);
let base64code = Buffer.from(bitmap, "binary").toString("base64");
const dataUri = `[p ]:data:image/${extname};base64,${base64code}`;
fs.appendFile(filePath, dataUri, null, () => {});
